package com.jxl.application.controller.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.common.base.Preconditions;
import com.jxl.application.controller.convert.AuthUserDtoConvert;
import com.jxl.common.util.ContextUtil;
import com.jxl.dto.Result;
import com.jxl.domain.entity.AuthUserBO;
import com.jxl.domain.service.AuthUserDomainService;
import com.jxl.dto.AuthUserDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户模块
 */
@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {

    @Resource
    private AuthUserDomainService authUserDomainService;

    /**
     * 用户注册
     * @param authUserDto
     * @return
     */
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody AuthUserDto authUserDto){
        try{
            AuthUserBO authUserBO =
                    AuthUserDtoConvert.INSTANCE.convertDtoToAuthUserBO(authUserDto);
            Boolean register = authUserDomainService.register(authUserBO);
            return Result.ok(register);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 用户修改
     * @param authUserDto
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthUserDto authUserDto){
        try{
            AuthUserBO authUserBO =
                    AuthUserDtoConvert.INSTANCE.convertDtoToAuthUserBO(authUserDto);
            Boolean register = authUserDomainService.update(authUserBO);
            return Result.ok(register);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 用户删除
     * @param authUserDto
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthUserDto authUserDto){
        try{
            AuthUserBO authUserBO =
                    AuthUserDtoConvert.INSTANCE.convertDtoToAuthUserBO(authUserDto);
            Boolean register = authUserDomainService.delete(authUserBO);
            return Result.ok(register);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 修改用户状态
     * @param authUserDto
     * @return
     */
    @PostMapping("/updateStatus")
    public Result<Boolean> updateStatus(@RequestBody AuthUserDto authUserDto){
        try{
            AuthUserBO authUserBO =
                    AuthUserDtoConvert.INSTANCE.convertDtoToAuthUserBO(authUserDto);
            Boolean register = authUserDomainService.updateStatus(authUserBO);
            return Result.ok(register);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 用户登录
     * @param validCode
     * @return
     */
    @GetMapping("doLogin")
    public Result<SaTokenInfo> doLogin(@RequestParam("validCode") String validCode) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(validCode), "验证码不能为空!");
            return Result.ok(authUserDomainService.doLogin(validCode));
        } catch (Exception e) {
            //log.error("UserController.doLogin.error:{}", e.getMessage(), e);
            return Result.fail("用户登录失败");
        }
    }


    /**
     * 验证是否登录
     * @return
     */
    @GetMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }


    /**
     * 用户退出
     */
    @RequestMapping("logOut")
    public Result logOut(@RequestParam String userName) {
        try {
            //log.info("UserController.logOut.userName:{}", userName);
            Preconditions.checkArgument(!StringUtils.isBlank(userName), "用户名不能为空");
            StpUtil.logout(userName);
            return Result.ok();
        } catch (Exception e) {
            //log.error("UserController.logOut.error:{}", e.getMessage(), e);
            return Result.fail("用户登出失败");
        }
    }

    /**
     * 获取用户信息
     */
    @RequestMapping("getUserInfo")
    public Result<AuthUserDto> getUserInfo(@RequestBody AuthUserDto authUserDTO) {
        try {
            Preconditions.checkArgument(!StringUtils.isBlank(authUserDTO.getUserName()), "用户名不能为空");
            String userLoginId = ContextUtil.getUserLoginId();
            AuthUserBO authUserBO = AuthUserDtoConvert.INSTANCE.convertDtoToAuthUserBO(authUserDTO);
            AuthUserBO userInfo = authUserDomainService.getUserInfo(authUserBO);
            return Result.ok(AuthUserDtoConvert.INSTANCE.convertBOToAuthUserDto(userInfo));
        } catch (Exception e) {
            //log.error("UserController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新用户信息失败");
        }
    }


}
